home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ultra250.zip / UW_TUT7.C < prev    next >
Text File  |  1992-11-02  |  17KB  |  444 lines

  1. /****************************************************************************/
  2. /* UW_TUT7.C                                                                */
  3. /*                                                                          */
  4. /* NOTE: THIS FILE IS PUBLIC DOMAIN AND MAY BE MODIFIED AND USED AT WILL    */
  5. /*                                                                          */
  6. /* Now we add background printing, allowing us to print a single record     */
  7. /* or all of the records.  We will add another drop down menu to do this.   */
  8. /* Since we are already calling a background function to display the real-  */
  9. /* time clock, we cannot set the idle function to print.  However, we can   */
  10. /* simply let the current idle function, "disp_time", call "print_in_bkgrnd"*/
  11. /* Using this simple method, we can perform many tasks in the background!   */
  12. /*   Notice that we print to LPT1 and make no arrangements for DOS critical */
  13. /* errors!  We are  trying to show the use of the library as opposed to     */
  14. /* ideal guidelines for application development and don't want to complicate*/
  15. /* this mini-app.                                                           */
  16. /*                                                                          */
  17. /*                                                         Dr. Boyd Gafford */
  18. /*                                                         Kevin Huck       */
  19. /*                                                         EnQue Software   */
  20. /*                                                         09/11/92         */
  21. /****************************************************************************/
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #include <io.h>
  25. #ifndef __TURBOC__
  26. #include <sys\types.h>
  27. #endif
  28. #include <sys\stat.h>
  29. #include <time.h>
  30. #include <ctype.h>
  31. #include "uw.h"                           /* include the necessary headers  */
  32.  
  33. #define MAX_CUST 100
  34.  
  35. typedef struct cust
  36. {
  37.   int status;
  38.   int cust_no;
  39.   char business[34];
  40.   char name[34];
  41.   char addr[34];
  42.   char city[34];
  43.   char state[4];
  44.   char zip[10];
  45.   char phone[16];
  46.   char fax[16];
  47.   char date[10];
  48.   char memo[34];
  49.   char unused[26];                                /* round out to 256 bytes */
  50. } CUST;
  51.  
  52. /*----------------------- global window variables --------------------------*/
  53. WINDOW   Desk_wn, Window1;
  54. CUST Customers[MAX_CUST];
  55. char Fname[33];
  56.  
  57. MENU    Top_menu, *Top_mnp = &Top_menu;
  58. MENU    Files_menu, Edit_menu, Print_menu;
  59. MENU    *Drop_mnps[3];
  60.  
  61. PRINT   Print;
  62.  
  63. /*-------------------------------- prototypes ------------------------------*/
  64. int disp_time(void);
  65. void disp_cust(CUST *cp, WINDOW *wnp);
  66. int file_load(CUST *customers);
  67. int file_save(CUST *customers);
  68. int get_fname(char *fname);
  69. void print_cust(CUST *cp, PRINT *p);
  70.  
  71. /*********/
  72. /* ~main */
  73. /*       ********************************************************************/
  74. /*  Demonstrate data entry capability...                                    */
  75. /****************************************************************************/
  76. int main()
  77. {
  78.   int i, ret_val, cust = 0, end_flag = 0, print_stat = 0;
  79.   WINDOW *wnp;
  80.   CUST *cp;
  81.   uchar back_att  = (LIGHTGRAY << 4) | BLACK,
  82.         bdr_att   = (LIGHTGRAY << 4) | BLACK,
  83.         csr_att   = (CYAN << 4) | YELLOW,
  84.         first_att = (LIGHTGRAY << 4) | RED;
  85.   
  86.   wnp = &Window1;                         /* set local window pointer       */
  87.   init_video(80, 25);                     /* init video for 80 x 25 screen  */
  88.   init_clock(0x3333);                     /* init clock irq at 91 tics/sec  */
  89.  
  90.   wn_create(0, 0, V_cols-1, V_rows-1, NO_BDR, WN_NORMAL, &Desk_wn);
  91.   link_window(&Desk_wn);
  92.  
  93.   /*------------------------ create the menu system ------------------------*/
  94.   Drop_mnps[0] = &Files_menu;
  95.   Drop_mnps[1] = &Edit_menu;
  96.   Drop_mnps[2] = &Print_menu;
  97.  
  98.   menu_create(0, 0, V_cols - 1, 0, M_HORIZONTAL,
  99.               back_att, bdr_att, csr_att, first_att,
  100.               NO_BDR, WN_NORMAL, Top_mnp);
  101.   item_add( "   Files   ", 1, 3, &Top_menu );
  102.   item_add( "   Edit    ", 2, 3, &Top_menu );
  103.   item_add( "   Print   ", 8, 3, &Top_menu );
  104.  
  105.   menu_create(0, 1, 14, 5, M_VERTICAL,
  106.     back_att, bdr_att, csr_att, first_att,
  107.     SGL_BDR, WN_NORMAL, Drop_mnps[0]);
  108.   item_add( " Load File", 4, 1, &Files_menu );
  109.   item_add( " Save File", 5, 1, &Files_menu );
  110.   item_add( "   Quit   ", 3, 3, &Files_menu );
  111.  
  112.   menu_create(11, 1, 32, 4, M_VERTICAL,
  113.     back_att, bdr_att, csr_att, first_att,
  114.     SGL_BDR, WN_NORMAL, Drop_mnps[1]);
  115.   item_add( " Clear Current", 6, 7, &Edit_menu );
  116.   item_add( " Clear All    ", 7, 7, &Edit_menu );
  117.  
  118.   menu_create(22, 1, 43, 4, M_VERTICAL,
  119.     back_att, bdr_att, csr_att, first_att,
  120.     SGL_BDR, WN_NORMAL, Drop_mnps[2]);
  121.   item_add( " Print Current", 9, 7, &Print_menu );
  122.   item_add( " Print All    ", 10, 7, &Print_menu );
  123.  
  124.   set_idle_func(disp_time);               /* set background clock function  */
  125.  
  126.   wn_create(5, 5, 75, 20, SLD_BDR, WN_POPUP, wnp);
  127.   wn_color(YELLOW, BLUE, wnp);            /* change the window colors       */
  128.   wn_bdr_color(WHITE, BLUE, wnp);         /* change the border's colors     */
  129.   link_window(wnp);
  130.  
  131.   /*------------- initialize first customer as EnQue Software --------------*/
  132.   cp = &Customers[0];
  133.   strcpy(cp->business, "EnQue Software");
  134.   strcpy(cp->name, "Kevin Huck & Boyd Gafford");
  135.   strcpy(cp->addr, "Rt. 1 Box 116C");
  136.   strcpy(cp->city, "Pleasant Hill");
  137.   strcpy(cp->state, "MO");
  138.   strcpy(cp->zip, "64080");
  139.   strcpy(cp->phone, "(816)987-2515");
  140.   strcpy(cp->fax, "(816)987-2515");
  141.   strcpy(cp->date, "09/11/92");
  142.   strcpy(cp->memo, "BBS 816-358-8990");
  143.  
  144.   /*------------------------ initialize the printer ------------------------*/
  145.   if( init_printer("LPT1", NULL, 2048L, 2048L, &Print) )
  146.     print_stat = 1;
  147.  
  148.   menu_set(Top_mnp);
  149.   while(!end_flag)
  150.   {
  151.     cp = &Customers[cust];
  152.     mv_cs(1,1, wnp);
  153.     wn_printf(wnp, "Customer:%3d", cust+1);
  154.     wn_plst(CENTERED, 3, "Use cursor keypad to select customer", &Desk_wn);
  155.     disp_cust(cp, wnp);
  156.     wait_event();
  157.     switch(Event.key)
  158.     {
  159.       /*------------------------- process menus ----------------------------*/
  160.       case KEY_ALT_Q:                                       /* quit program */
  161.         end_flag = 1;
  162.         break;
  163.       case KEY_ALT_F: case KEY_ALT_E: case KEY_ALT_P:
  164.         m_show();
  165.         if( Event.key == KEY_ALT_F )
  166.           ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'F',M_EXIT_ON_ESC);
  167.         else if( Event.key == KEY_ALT_E )
  168.           ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'E',M_EXIT_ON_ESC);
  169.         else
  170.           ret_val = menu_system_ll(&Top_menu,&Drop_mnps[0],0,'P',M_EXIT_ON_ESC);
  171.         switch( ret_val )
  172.         {
  173.           case 3:                                           /* quit program */
  174.             end_flag = 1;
  175.             break;
  176.           case 4:                                           /* load file    */
  177.             file_load(Customers);
  178.             break;
  179.           case 5:                                           /* save file    */
  180.             file_save(Customers);
  181.             break;
  182.           case 6:                                           /* clear one    */
  183.             setmem(cp, sizeof(CUST), 0);
  184.             break;
  185.           case 7:                                           /* clear all    */
  186.             setmem(Customers, sizeof(Customers), 0);
  187.             break;
  188.           case 9:                                           /* print one    */
  189.             if( print_stat )
  190.               print_cust(cp, &Print);
  191.             break;
  192.           case 10:                                          /* print all    */
  193.             if( print_stat )
  194.               for( i = 0; i < MAX_CUST; i++ )
  195.                 if( strlen(Customers[i].name) )             /* not empty?   */
  196.                   print_cust(&Customers[i], &Print);
  197.             break;
  198.         }
  199.         break;
  200.       /*---------------------- process cursor keys -------------------------*/
  201.       case KEY_HOME:
  202.         cust = 0;
  203.         break; 
  204.       case KEY_END:
  205.         cust = MAX_CUST-1;
  206.         break; 
  207.       case KEY_DN: 
  208.         if( cust < MAX_CUST-1 )
  209.           cust++;
  210.         break;
  211.       case KEY_UP: 
  212.         if( cust > 0 )
  213.           cust--;
  214.         break;
  215.       case KEY_PGUP: 
  216.         if( cust >= 10 )
  217.           cust -= 10;
  218.         else
  219.           cust = 0;
  220.         break;
  221.       case KEY_PGDN: 
  222.         if( cust < MAX_CUST-11 )
  223.           cust += 10;
  224.         else
  225.           cust = MAX_CUST-1;
  226.         break;
  227.       /*-------------------- process field edit keys -----------------------*/
  228.       case 'b': case 'B':                         /* get new business name  */
  229.         mv_cs( 11, 3, wnp);
  230.         wn_gets_ll(cp->business, "________________________________",
  231.                                  "********************************",
  232.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  233.         break;
  234.       case 'n': case 'N':                         /* get new contact name   */
  235.         mv_cs( 11, 4, wnp);
  236.         wn_gets_ll(cp->name, "________________________________",
  237.                              "********************************",
  238.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  239.         break;
  240.       case 'a': case 'A':                         /* get new address        */
  241.         mv_cs( 11, 5, wnp);
  242.         wn_gets_ll(cp->addr, "________________________________",
  243.                              "********************************",
  244.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  245.         break;
  246.       case 'c': case 'C':                         /* get new city           */
  247.         mv_cs( 11, 6, wnp);
  248.         wn_gets_ll(cp->city, "________________________________",
  249.                              "********************************",
  250.           swap_nibbles(wnp->att), G_UP_FST_CHAR2 | G_STRIP_END, 32, wnp);
  251.         break;
  252.       case 's': case 'S':                         /* get new state          */
  253.         mv_cs( 51, 6, wnp);
  254.         wn_gets_ll(cp->state, "__", "UU",
  255.           swap_nibbles(wnp->att), G_EXIT_ON_FILL|G_STRIP_END, 2, wnp);
  256.         break;
  257.       case 'z': case 'Z':                         /* get new zip            */
  258.         mv_cs( 60, 6, wnp);
  259.         wn_gets_ll(cp->zip, "_____", "#####",
  260.           swap_nibbles(wnp->att), G_EXIT_ON_FILL|G_STRIP_END, 5, wnp);
  261.         break;
  262.       case 'p': case 'P':                         /* get new phone number   */
  263.         mv_cs( 11, 7, wnp);
  264.         wn_gets_ll(cp->phone, "(___)___-____", " ### ### ####",
  265.           swap_nibbles(wnp->att), G_EXIT_ON_FILL, 14, wnp);
  266.         break;                    
  267.       case 'f': case 'F':                         /* get new fax number     */
  268.         mv_cs( 11, 8, wnp);
  269.         wn_gets_ll(cp->fax, "(___)___-____", " ### ### ####",
  270.           swap_nibbles(wnp->att), G_EXIT_ON_FILL, 14, wnp);
  271.         break;
  272.       case 'd': case 'D':                         /* get new date           */
  273.         mv_cs( 11, 9, wnp);
  274.         wn_gets_ll(cp->date, "__/__/__", "## ## ##", swap_nibbles(wnp->att),
  275.           G_EXIT_ON_FILL, 8, wnp);
  276.         break;
  277.       case 'm': case 'M':                         /* get new memo field     */
  278.         mv_cs( 11, 10, wnp);
  279.         wn_gets_ll(cp->memo, "________________________________",
  280.                              "********************************",
  281.           swap_nibbles(wnp->att), G_STRIP_END, 32, wnp);
  282.         break;
  283.  
  284.     }
  285.   }
  286.  
  287.   unlink_window(wnp);                     /* remove the window from screen  */
  288.   wn_destroy(wnp);
  289.   set_idle_func(NULL);                    /* remove background function     */
  290.   unlink_window(&Desk_wn);                /* remove the window from screen  */
  291.   wn_destroy(&Desk_wn);
  292.   end_clock();
  293.   end_video();                            /* clean up before we exit        */
  294.   end_printer(&Print);
  295.   return(1);
  296. }
  297. /*** end of main ***/
  298.  
  299. /**************/
  300. /* ~disp_time */
  301. /*            ***************************************************************/
  302. /*  This routine is called in the background by wait_event and will display */
  303. /*  the time once per second.  Notice the use of the global variables       */
  304. /*  Uw_timers.  There is an array of four "countdown" timers that are user  */
  305. /*  accessible.  Each "timer tic" will decrement the counts by one, until   */
  306. /*  0 is reached.  By "reloading" the timer with "Tics_per_sec", we only    */
  307. /*  display the time of day once per second.  "Tics_per_sec" is set         */
  308. /*  by init_clock.                                                          */
  309. /****************************************************************************/
  310. int disp_time(void)
  311. {
  312.   time_t t;
  313.   print_in_bkgrnd();                            /* call this to print       */
  314.   if( !Uw_timers[0] )                           /* has one second passed?   */
  315.   {
  316.     Uw_timers[0] = Tics_per_sec;                /* if so, reload timer      */
  317.     t = time(NULL);                             /* get time                 */
  318.     mv_cs(55, V_rows-1, &Desk_wn);              /* move window cursor       */
  319.     wn_st_qty(ctime(&t), 24, &Desk_wn);         /* output 24 characters     */
  320.     return(1);
  321.   }
  322.   return(0);
  323. }
  324. /*** end of disp_time ***/
  325.  
  326. /**************/
  327. /* ~disp_cust */
  328. /*            ***************************************************************/
  329. /*  This routine displays a customer in the desired window...               */
  330. /****************************************************************************/
  331. void disp_cust(CUST *cp, WINDOW *wnp)
  332. {
  333.   int r = 3;
  334.  
  335.   mv_cs( 1, r++, wnp );
  336.   wn_printf( wnp, "Business: %-32s", cp->business);
  337.   mv_cs( 1, r++, wnp );
  338.   wn_printf( wnp, "Name    : %-32s", cp->name );
  339.   mv_cs( 1, r++, wnp );
  340.   wn_printf( wnp, "Address : %-32s", cp->addr );
  341.   mv_cs( 1, r++, wnp );
  342.   wn_printf( wnp, "City    : %-32s State: %2s  Zip: %5s",
  343.     cp->city, cp->state, cp->zip );
  344.   wn_cleol(wnp);                                    /* clear to end of line */
  345.   mv_cs( 1, r++, wnp );
  346.   wn_printf( wnp, "Phone   : %-16s", cp->phone );
  347.   mv_cs( 1, r++, wnp );
  348.   wn_printf( wnp, "Fax     : %-16s", cp->fax );
  349.   mv_cs( 1, r++, wnp );
  350.   wn_printf( wnp, "Date    : %-10s", cp->date );
  351.   mv_cs( 1, r++, wnp );
  352.   wn_printf( wnp, "Memo    : %-32s", cp->memo );
  353. }
  354. /*** end of disp_cust ***/
  355.  
  356. /**************/
  357. /* ~file_load */
  358. /*            ***************************************************************/
  359. /*  This routine loads a file from disk into the customer array...          */
  360. /****************************************************************************/
  361. int file_load(CUST *customers)
  362. {
  363.   FILE *fp;
  364.   
  365.   if( get_fname(Fname) )
  366.   {
  367.     if( (fp = fopen(Fname, "rb")) != NULL )
  368.     {
  369.       fread(customers, sizeof(CUST), MAX_CUST, fp);
  370.       fclose(fp);
  371.       tone(1024,10);
  372.       return(1);
  373.     }
  374.   }
  375.   return(0);
  376. }
  377. /*** end of file_load ***/
  378.  
  379. /**************/
  380. /* ~file_save */
  381. /*            ***************************************************************/
  382. /*  This routine saves a file to disk from the customer array...            */
  383. /****************************************************************************/
  384. int file_save(CUST *customers)
  385. {
  386.   FILE *fp;
  387.   
  388.   if( get_fname(Fname) )
  389.   {
  390.     if( (fp = fopen(Fname, "wb")) != NULL )
  391.     {
  392.       fwrite(customers, sizeof(CUST), MAX_CUST, fp);
  393.       fclose(fp);
  394.       tone(1024,10);
  395.       return(1);
  396.     }
  397.   }
  398.   return(0);
  399. }
  400. /*** end of file_load ***/
  401.  
  402. /**************/
  403. /* ~get_fname */
  404. /*            ***************************************************************/
  405. /*  This routine prompts the user for a filename using a popup window...    */
  406. /****************************************************************************/
  407. int get_fname(char *fname)
  408. {
  409.   int ret_val = 1;
  410.   WINDOW wn;
  411.   
  412.   wn_create(20, 8, 60, 10, SLD_BDR, WN_POPUP, &wn);
  413.   wn_set(&wn);
  414.   wn_plst(4, 0, "Enter filename:", &wn);
  415.   if( wn_gets_ll(fname, "____________", "************",
  416.       swap_nibbles(wn.att), G_STRIP_END, 12, &wn) == KEY_ESC )
  417.     ret_val = 0;
  418.   wn_destroy(&wn);
  419.   return(ret_val);
  420. }
  421. /*** end of get_fname ***/
  422.  
  423. /***************/
  424. /* ~print_cust */
  425. /*             **************************************************************/
  426. /*  This routine prints a customer in the desired window...                 */
  427. /****************************************************************************/
  428. void print_cust(CUST *cp, PRINT *p)
  429. {
  430.   print_printf( p, "Business: %-32s\r\n", cp->business);
  431.   print_printf( p, "Name    : %-32s\r\n", cp->name );
  432.   print_printf( p, "Address : %-32s\r\n", cp->addr );
  433.   print_printf( p, "City    : %-32s State: %2s  Zip: %5s\r\n",
  434.     cp->city, cp->state, cp->zip );
  435.   print_printf( p, "Phone   : %-16s\r\n", cp->phone );
  436.   print_printf( p, "Fax     : %-16s\r\n", cp->fax );
  437.   print_printf( p, "Date    : %-10s\r\n", cp->date );
  438.   print_printf( p, "Memo    : %-32s\r\n", cp->memo );
  439.   print_char(12,p);                                     /* print form feed  */
  440. }
  441. /*** end of print_cust ***/
  442.  
  443. /*** END OF FILE ***/
  444.